from selenium import webdriver
import time

#无界面模式
# option = webdriver.ChromeOptions()
# option.add_argument('--headless')

driver = webdriver.Chrome()

driver.get('http://www.baidu.com')
time.sleep(2)
#通过id获取元素，找到搜索框
ele = driver.find_element_by_id('kw')
#输入内容
ele.send_keys('selenium')
time.sleep(2)
ele2 = driver.find_element_by_id('su')
#点击
ele2.click()
time.sleep(3)
#页面滚动到底部
js2 = 'document.documentElement.scrollTop = 8000'
#页面横向滚动0像素，纵向滚动700像素
js3 = 'window.scrollTo(0,700)'
driver.execute_script(js3)
time.sleep(2)
#通过执行js开启新页面
js = 'window.open("https://www.sougou.com")'
driver.execute_script(js)
time.sleep(2)
#获取所有窗口
windows = driver.window_handles
#根据索引进行切换
driver.switch_to.window(windows[1])
time.sleep(3)
driver.switch_to.window(windows[0])
time.sleep(3)

print(driver.title)
#打印浏览器渲染后的网页源代码
# print(driver.page_source)
#网页截图
# driver.save_screenshot('asd.png')
print(driver.current_url)
driver.close()
time.sleep(1)
driver.quit()